﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using BulldogCMS.Core.Storage;
using BulldogCMS.Core.Storage.DataTypes;

namespace BulldogCMS.Core.Tests.Configuration
{
    [TestClass]
    public class ConfigurationLoaderTest
    {
        [TestMethod]
        public void AppContextInitialized()
        {
            Assert.IsNotNull(Context.Application, "Application context not initialized");
        }

        [TestMethod]
        public void AppContextInitializedProperties()
        {
            Assert.IsNotNull(Context.Application.Tree, "Storage tree not initialized");
            Assert.IsNotNull(Context.Application.UniqueIdentifierProvider, "UniqueIdentifierProvider not initialized");
            Assert.IsNotNull(Context.Application.URLResolver, "URLResolver not initialized");
            Assert.IsNotNull(Context.Application.RequestContextProvider, "RequestContextProvider not initialized");
            Assert.IsNotNull(Context.Application.StorageCache, "StorageCache not initialized");
        }

        [TestMethod]
        public void LoadTemplatesTest()
        {
            Assert.AreNotEqual(Context.Application.Templates.Count(), 0, "Zero templates");
            var firstTemplate = Context.Application.GetTemplate("BulldogCMS.Core.Templates.System.LanguageNodeTemplate");
            Assert.IsNotNull(firstTemplate);
        }

        [TestMethod]
        public void LoadLanguagesNode()
        {
            var languagesNode = Context.Application.Tree.GetNode("/System/Languages");
            Assert.IsNotNull(languagesNode);

            if (languagesNode != null)
            {
                var nodes = languagesNode.Nodes.ToList();
                Assert.IsNotNull(languagesNode);
                Assert.AreEqual<int>(2, nodes.Count);
                var czechLanguageNode = languagesNode.GetChild("cs-CZ");
                Assert.IsNotNull(czechLanguageNode);
                var displayName = czechLanguageNode.GetValue<TextField>("DisplayName");
                string displayName2 = czechLanguageNode.Bag.DisplayName.Value;
                Assert.AreEqual<string>(displayName.Value, displayName2);
                var englishName = czechLanguageNode.GetValue<TextField>("EnglishName");
                string englishName2 = czechLanguageNode.Bag.EnglishName.Value;
                Assert.AreEqual<string>(englishName.Value, englishName2);
            }
        }

        [TestMethod]
        public void LoadSitesTest()
        {
            Assert.AreEqual(Context.Application.Sites.Count(), 2, "Count of sites in configration file does not match expected count");
            Site firstSite = Context.Application.Sites.FirstOrDefault();
            if (firstSite != null)
            {
                Assert.AreEqual<string>(firstSite.HostName, "www.company.com");
                Assert.AreEqual<string>(firstSite.ContentPath, "/Content/ComputerShop");
                Assert.AreEqual<string>(firstSite.Language, "cs-CZ");
                Assert.AreEqual<SiteDefaultNodeVersion>(firstSite.DefaultNodeVersion, SiteDefaultNodeVersion.Published);
                Assert.AreEqual<int>(firstSite.Port, 80);
            }
            else
            {
                Assert.Inconclusive("Configuration does not contain any sites");
            }

            Site lastSite = Context.Application.Sites.LastOrDefault();
            if (lastSite != null)
            {
                Assert.AreEqual<string>(lastSite.HostName, "localhost");
                Assert.AreEqual<string>(lastSite.ContentPath, "/Content/Partners");
                Assert.AreEqual<string>(lastSite.Language, "en-GB");
                Assert.AreEqual<SiteDefaultNodeVersion>(lastSite.DefaultNodeVersion, SiteDefaultNodeVersion.LatestDraft);
                Assert.AreEqual<int>(lastSite.Port, 8080);
            }
        }

        #region init and cleanup

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            XDocument defaultTreeDoc = Helper.ReflectionHelper.GetXMLFromAssembly("BulldogCMS.Core.Tests.Resources.Trees.DefaultTestingTree.xml");
            XDocument defaultConfigurationDoc = Helper.ReflectionHelper.GetXMLFromAssembly("BulldogCMS.Core.Tests.Resources.Configurations.DefaultConfiguration.xml");

            BulldogCMS.Core.Context.InitApplicationContext(defaultConfigurationDoc, defaultTreeDoc);
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            BulldogCMS.Core.Context.CleanContext();
        }

        #endregion

    }
}
